package com.dongyun.loraweb.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dongyun.loraweb.dao.BaseStationPassRepository;
import com.dongyun.loraweb.dao.BaseStationRepository;
import com.dongyun.loraweb.dao.DeviceRepository;
import com.dongyun.loraweb.entity.LoraBaseStation;
import com.dongyun.loraweb.entity.LoraBaseStationPass;
import com.dongyun.loraweb.entity.LoraDevice;
import com.dongyun.loraweb.json.Grid;
import com.dongyun.loraweb.json.Result;
import com.dongyun.loraweb.json.Tree;
import com.dongyun.loraweb.util.StrUtil;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/device")
public class DeviceController extends AbstractController {
	private static Logger LOGGER = LoggerFactory.getLogger(DeviceController.class);
	@Autowired
	private DeviceRepository deviceRepository;
	@Autowired
	private BaseStationRepository baseStationRepository;
	@Autowired
	private BaseStationPassRepository baseStationPassRepository;
	@Autowired
	private HttpServletRequest req;
	
	@ApiOperation("添加基站")
	@RequestMapping(value="/addLoraBaseStation",method={RequestMethod.POST,RequestMethod.GET})
	public Result addLoraBaseStation(LoraBaseStation entity){
		if(StrUtil.isEmpty(entity.getIdentifier())||StrUtil.isEmpty(entity.getName())){
			return createResult("编号和名称不能为空！");
		}
		
		if (!StrUtil.isEmpty(entity.getUuid())) {
			if(baseStationRepository.findOne(entity.getUuid())==null){
				return createResult("基站不存在！");
			}
		}else{
//			entity.setUuid(null);
		}
		LoraBaseStation loraBaseStation = baseStationRepository.save(entity);
		return createResult(true,"保存成功",loraBaseStation.getUuid());
	}
	@ApiOperation("删除基站")
	@RequestMapping(value="/deleteBaseStation",method={RequestMethod.POST,RequestMethod.GET})
	public Result deleteBaseStation(@RequestParam(defaultValue="") String id){
		System.out.println(id);
		if(id==null){
			return createResult("id不能为空");	
		}
		baseStationRepository.delete(id);
		return createSuccessResult("删除成功！");
	}
	
	@ApiOperation("获取所有基站")
	@RequestMapping(value="/getAllLoraBaseStation",method={RequestMethod.POST,RequestMethod.GET})
	public List<Tree> findAddLoraBaseStation(){
		LOGGER.info("获取所有基站");
		List<LoraBaseStation> list = baseStationRepository.findAll();
		List<Tree> listTree=new ArrayList<>();
		for (LoraBaseStation loraBaseStation : list) {
			Tree tree = new Tree();
			tree.setText(loraBaseStation.toString());
			tree.setId(loraBaseStation.getUuid());
			tree.setAttributes(loraBaseStation);
			listTree.add(tree);
		}
		return listTree;
	}
	@ApiOperation("添加基站通道")
	@RequestMapping(value="/addLoraPass",method={RequestMethod.POST,RequestMethod.GET})
	public Result addLoraPass(@RequestParam(defaultValue="") String baseStationId,@RequestBody LoraBaseStationPass pass){
		if(StrUtil.isEmpty(baseStationId)||StrUtil.isEmpty(pass.getIdentifier())||StrUtil.isEmpty(pass.getName())){
			return createResult("编号和名称不能为空！");
		}
		LoraBaseStation entity = baseStationRepository.findOne(baseStationId);
		if(entity==null){
			return createResult("基站不存在！");
		}
		if(StrUtil.isEmpty(pass.getUuid())){
			LoraBaseStationPass loraBaseStationPass = baseStationPassRepository.findPassByLoraBaseStationAndPass(entity,pass.getPass());
			if(loraBaseStationPass!=null){
				return createResult("通道已存在!");
			}
		}else{
			LoraBaseStationPass loraBaseStationPass = baseStationPassRepository.findOne(pass.getUuid());
			if(StrUtil.isEmpty(loraBaseStationPass)){
				return createResult("通道不存在！");
			}
		}
		pass.setLoraBaseStation(entity);
		LoraBaseStationPass save = baseStationPassRepository.save(pass);
		return createSuccessResult("保存成功",save.getUuid());
	}
	@ApiOperation("删除基站通道")
	@RequestMapping(value="/deleteLoraPass",method={RequestMethod.POST,RequestMethod.GET})
	public Result deleteLoraPass(String id){
		baseStationPassRepository.delete(id);
		return createSuccessResult("删除成功!");
	}
	@ApiOperation("获取基站通道列表")
	@RequestMapping(value="/getAllPasses",method={RequestMethod.POST,RequestMethod.GET})
	public Grid getAllPasses(String baseStationId){
		LoraBaseStationPass probe = new LoraBaseStationPass();
		LoraBaseStation loraBaseStation = new LoraBaseStation();
		loraBaseStation.setUuid(baseStationId);
		probe.setLoraBaseStation(loraBaseStation);
		List<LoraBaseStationPass> list=baseStationPassRepository.findAll(Example.of(probe));
		return createGrid(list.size()*1l, list);
	}
	
	@ApiOperation("添加基站设备")
	@RequestMapping(value="/addDevice",method={RequestMethod.POST,RequestMethod.GET})
	public Result addLoraDevice(String passId,LoraDevice device){
		if(StrUtil.isEmpty(passId)||StrUtil.isEmpty(device.getIdentifier())||StrUtil.isEmpty(device.getName())){
			return createResult("数据不完整！");
		}
		LoraBaseStationPass loraBaseStationPass = baseStationPassRepository.findOne(passId);
		if(loraBaseStationPass==null){
			return createResult("基站通道不存在!");
		}
		
		if(!StrUtil.isEmpty(device.getUuid())){
			LoraDevice loraDevice = deviceRepository.findOne(device.getUuid());
			if(loraDevice==null){
				return createResult("设备不存在");
			}
			loraDevice.setIdentifier(device.getIdentifier());
			loraDevice.setName(device.getName());
			device=loraDevice;
		}
		device.setLoraBaseStationPass(loraBaseStationPass);
		deviceRepository.save(device);
		return createResult(true, "保存成功！");
	}
	@ApiOperation("删除基站设备")
	@RequestMapping(value="/deleteDevice",method={RequestMethod.POST,RequestMethod.GET})
	public Result deleteDevice(String id){
		deviceRepository.delete(id);
		return createSuccessResult("删除成功!");
	}
	
	@ApiOperation("获取基站设备列表")
	@RequestMapping(value="/getAllDevices",method={RequestMethod.POST,RequestMethod.GET})
	public Grid getAllDevices(String passId){
		LoraDevice probe=new LoraDevice();
		LoraBaseStationPass loraBaseStationPass = new LoraBaseStationPass();
		loraBaseStationPass.setUuid(passId);
		probe.setLoraBaseStationPass(loraBaseStationPass);
		List<LoraDevice> list=deviceRepository.findAll(Example.of(probe));
		return createGrid(list.size()*1l, list);
	}
}
